package leetcode.editor.cn;

import utils.TreeNode;

/**
 * 验证二叉搜索树
 * @author qulq
 * @date 2025-03-04 21:50:12
 */
public class T98_ValidateBinarySearchTree{
	 public static void main(String[] args) {
	 	 // 测试代码
	 	 Solution solution = new T98_ValidateBinarySearchTree().new Solution();
	 }
	 
// 力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
	public TreeNode maxNode;

    public boolean isValidBST(TreeNode root) {
		if (root == null) {
			return true;
		}
		// 判断左子树
		boolean left = isValidBST(root.left);
		if (!left) {
			return false;
		}
		// 判断当前节点
		if (maxNode != null && root.val <= maxNode.val) {
			return false;
		}
		maxNode = root;
		// 判断右子树
		return isValidBST(root.right);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}